#!/bin/bash 

###
#	@author: msune, cfernandez
#	@organization: i2CAT
#	@project: Ofelia FP7
#       @description: 
###

##GENERAL parameters
BASH="/bin/bash"
EXECPATH=$PWD
EXECPID=$$
LOG="$EXECPATH/log/`date +"%R-%d%m%y"`#$EXECPID"
BACKUP_PATH="$EXECPATH/rescue/`date +"%R-%d%m%y"`#$EXECPID"
DEBUG=0
USE_LOG=1
BACKUP=1
FORCE=0
#Enable/disable ofver to call itself (through a son process) without being blocked
ENABLE_OFVER_SON_PROCESSES=1

##STATIC CONSTANTS
NO_RETURN=1
DONT_ABORT=2
NO_RESCUE=1
UPGRADE_REG=".upgradeReg"

##Other info
APP_NAME="ofver"
APP_VER="0.23"
APP_DESC="OFelia VERsioning system; a simple set of bash scripts to simplify repository based installations and upgrades."
APP_AUTHOR="{ marc.sune, leonardo.bergesio, carolina.fernandez } @i2cat.net (i2CAT foundation)"
APP_WEB="http://code.google.com/p/ofver/, http://www.fp7-ofelia.eu"


##Main method
function main()
{
	#Check root
	if [ "$(id -u)" != "0" ]; then
		echo "$(tput setaf 1)FATAL ERROR: $(tput sgr0)$APP_NAME must be run by root user"
		exit 1
	fi

	##Load settings
	source "$EXECPATH/settings" || ( echo "FATAL ERROR: Cannot import settings" && exit 1 )	

	##Load libraries
	source "$EXECPATH/common/utils" || ( echo "FATAL ERROR: Cannot import utils library" && exit 1 )
	source "$EXECPATH/common/init" || error "Cannot import init library" $NO_RESCUE
	source "$EXECPATH/common/versioning" ||error "Cannot import commands library" $NO_RESCUE
	source "$EXECPATH/common/modules" ||error "Cannot import module library" $NO_RESCUE
	source "$EXECPATH/common/commands" ||error "Cannot import commands library" $NO_RESCUE
	source "$EXECPATH/common/repository" ||error "Cannot import repository library" $NO_RESCUE

	lock

	#Check in_branch flag
	varChecks

	#Arguments
	checkArgsAndParseOpts $@

	#Try to recover upgrade from the 2 step process
	recoverUpgradeState

	#Splash
	splash 
	
	#Invoque action
	eval $ACTION_

	#Finish
	unlock
	rm -rf $UPGRADE_REG	

	print "Finishing execution..."
	exit 0		
}


#Calling main
main $@
